package com.bob.demo.utils;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;

public class StringUtils {

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
	
	public static boolean isNullOrEmpty(String str) {
		if (str == null || "".equals(str) || "null".equals(str)) {
			return true;
		}
		
		return false;
		
	}
	
	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		String uuidStr = uuid.toString().replace("-", "");
		return uuidStr;
	}
	
	public static String getResultMsg(Boolean success, String code, String msg) {
		JSONObject jo = new JSONObject();
		jo.put("success", success);
		jo.put("code", code + "");
		jo.put("msg", msg);

		return jo.toString();
	}

	public static String getResultMsg(Boolean success, String code, String msg,
			Map<String, Object> data) {
		JSONObject jo = new JSONObject();
		jo.put("success", success);
		jo.put("code", code);
		jo.put("msg", msg);

		for (String key : data.keySet()) {
			jo.put(key, data.get(key));
		}

		return jo.toString();
	}
	
	@SuppressWarnings("rawtypes")
	public static Map<String, String> getReqParams(HttpServletRequest request) {
		Map<String, String> params = new HashMap<String, String>();
		try {
			Enumeration enu = request.getParameterNames();
			while(enu.hasMoreElements()) {
				String paraName = (String)enu.nextElement();
				params.put(paraName, request.getParameter(paraName));
			}
			
		} catch (Exception e) {
		}
		
		return params;
	}
	
	public static byte[] hex2byte(String s) {
		byte[] src;
		byte[] ret = null;
		try {
			src = s.toLowerCase().getBytes("UTF-8");
			ret = new byte[src.length / 2];
			for (int i = 0; i < src.length; i += 2) {
				byte hi = src[i];
				byte low = src[i + 1];
				hi = (byte) ((hi >= 'a' && hi <= 'f') ? 0x0a + (hi - 'a')
						: hi - '0');
				low = (byte) ((low >= 'a' && low <= 'f') ? 0x0a + (low - 'a')
						: low - '0');
				ret[i / 2] = (byte) (hi << 4 | low);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return ret;
	}

	/**
	 * 格式化byte
	 * 
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte[] b) {
		char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		char[] out = new char[b.length * 2];
		for (int i = 0; i < b.length; i++) {
			byte c = b[i];
			out[i * 2] = digit[(c >>> 4) & 0X0F];
			out[i * 2 + 1] = digit[c & 0X0F];
		}

		return new String(out);
	}

	 /**
     * 前六后四 隐藏银行卡号
     *
     * @param cardNo
     * @return java.lang.String
     * @Date:16:57 2017/11/7
     */
    public static String hideCardNo(String cardNo) {
        if (cardNo.length() > 10) {
            //前六后四
            StringBuilder stringBuilder = new StringBuilder();
            return stringBuilder.append("**** **** **** ").append(cardNo.substring(cardNo.length() - 4)).toString();
        } else {
            return cardNo;
        }
    }
    

	public static String URLEncoder(String str) {
		String result = str;
		try {
			result = java.net.URLEncoder.encode(result, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
    public static void main(String[] args) {
		System.err.println(hideCardNo("622680001111111"));
	}
}
